
package hasthi.manager.gui;

import hasthi.common.HasthiException;
import hasthi.metamodel.resource.ManagedResource;
import hasthi.metamodel.resource.SummerizedManagedResource;
import hasthi.metamodel.resource.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.DefaultTreeCellRenderer;

public class ResourcePannel extends JPanel implements ActionListener{
    private ResourceTableModel tableModel;
    public ResourcePannel() {
        super(new BorderLayout());
    }
    
    public void setResource(ManagedResource resource){
        
        if(tableModel == null){
            tableModel = new ResourceTableModel(resource);
            JTable table = new JTable(tableModel);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            //table.setFillsViewportHeight(true);

            //Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);

            //Add the scroll pane to this panel.
            add(scrollPane,BorderLayout.CENTER);
            
            JButton button = new JButton("GetAllProperties");
            button.addActionListener(this);
            JPanel panel = new JPanel(new FlowLayout());
            panel.add(button);
            add(panel,BorderLayout.SOUTH);
            validate();
        }else{
            tableModel.setResource(resource);
        }
    }
    

    public class ResourceTableModel extends AbstractTableModel{
        private ManagedResource resource;
        private List<String> propertyNames;
        
        public ResourceTableModel(ManagedResource resource){
            propertyNames = new ArrayList<String>(resource.getProperties());
            this.resource = resource;
        }
        
        
        public int getColumnCount() {
            return 2;
        }

        public int getRowCount() {
            return propertyNames.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            try {
                if(columnIndex == 0){
                    return propertyNames.get(rowIndex);
                }else{
                    return resource.getProperty(propertyNames.get(rowIndex));
                }
            } catch (HasthiException e) {
                e.printStackTrace();
                return null;
            }
        }

        public void setResource(ManagedResource resource){
            propertyNames = new ArrayList<String>(resource.getProperties());
            this.resource = resource;
        }
        

        @Override
        public String getColumnName(int column) {
            return column == 0 ? "Name":"Value";
        }


        public ManagedResource getResource() {
            return resource;
        }
        
        public void update(){
            propertyNames = new ArrayList<String>(resource.getProperties());
            tableModel.fireTableDataChanged();
        }
        
    }


    public void actionPerformed(ActionEvent e) {
        try {
            ManagedResource resource = tableModel.getResource();
            SummerizedManagedResource underlineResource = null;
            if(resource instanceof SummerizedManagedResource){
                underlineResource = (SummerizedManagedResource)resource;
            }else if(resource instanceof ManagedService 
                    && ((ManagedService)resource).getRawResource() instanceof SummerizedManagedResource){
                underlineResource = (SummerizedManagedResource) ((ManagedService)resource).getRawResource();
            }
            
            if(underlineResource != null){
                underlineResource.readAllProperties();
            }
            tableModel.update();
            repaint();
        } catch (HasthiException e1) {
            e1.printStackTrace();
        }
    }
    
    public class ResourceRenderer extends DefaultTreeCellRenderer{
        private ImageIcon hostIcon;
        private ImageIcon serviceIcon;
        public ResourceRenderer(){
            
        }
        
        
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Component renderer = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if(ResourceNode.NodeType.Host.equals(((ResourceNode)value).getType())){
                
            }
            return renderer;
        }
        
        public ImageIcon createImageIcan(String path){
            URL url = getClass().getResource(path);
            if(url != null){
                return new ImageIcon(url);
            }else{
                return null;
            }
        }
    }
}

